﻿Imports Npgsql

Public Class Racas
    Private comando As New NpgsqlCommand()
    Private bd As New ConexaoBd()
    Private tblRaca As DataTable
    Private tRow As DataRow
    Private dset As DataSet
    Private limpar As New LimparDados()



    Private Sub btnSalvar_Click(sender As System.Object, e As System.EventArgs) Handles btnSalvar.Click

        Try
            If Val(txtCod.Text.ToString()) > 0 Then
                comando.CommandText = "UPDATE racas SET nome = @nome WHERE idRaca = @idRaca;"

                comando.Parameters.Add("@nome", txtNome.Text)
                comando.Parameters.Add("@idRaca", txtCod.Text)

                bd.atualizarDados(comando)

            Else
                comando.CommandText = "INSERT INTO racas(idraca, nome) VALUES (DEFAULT, @nome);"

                comando.Parameters.Add("@nome", txtNome.Text)
                bd.adicionarDados(comando)


                Dim sql As String = "SELECT MAX(idraca) id FROM racas"

                'Realizando consulta
                dset = bd.carregarDados(sql, "idraca")
                tblRaca = dset.Tables.Item(0)
                dset.Dispose()
                tRow = tblRaca.Rows(0)
                Dim id As Integer
                id = CInt(tRow("id").ToString())
                carregarRaca(id)

            End If

            'limpar.Limpar(Me)
            'txtCod.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub carregarRaca(ByVal id As Integer)
        Dim sql As String = "SELECT * FROM racas WHERE idraca = " + id.ToString()

        'Realizando consulta
        dset = bd.carregarDados(sql, "racas")
        tblRaca = dset.Tables.Item(0)
        dset.Dispose()

        If tblRaca.Rows.Count() = 1 Then
            tRow = tblRaca.Rows(0)

            txtCod.Text = tRow("idraca").ToString()
            txtNome.Text = tRow("nome").ToString()
            btnExcluir.Enabled = True
        Else
            MessageBox.Show("Raca Não Encontrada!")
            btnExcluir.Enabled = False
            limpar.Limpar(Me)
        End If


    End Sub

    Private Sub txtCod_Leave(sender As System.Object, e As System.EventArgs) Handles txtCod.Leave
        If txtCod.Text.Length > 0 And Val(txtCod.Text) > 0 Then
            carregarRaca(Val(txtCod.Text))
        End If
    End Sub

    Private Sub txtCod_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCod.KeyDown
        If e.KeyCode = 13 Then
            txtNome.Focus()
        End If
    End Sub

    Private Sub btnPesquisar_Click(sender As System.Object, e As System.EventArgs) Handles btnPesquisar.Click

        Dim sql As String = "SELECT * FROM racas"
        dset = bd.carregarDados(sql, "racas")

        grdRacas.DataSource = dset.Tables("racas").DefaultView


        grdRacas.Columns(0).HeaderText = "Código"
        grdRacas.Columns(1).HeaderText = "Nome"

    End Sub

    Private Sub grdRacas_RowHeaderMouseDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdRacas.RowHeaderMouseDoubleClick

        carregarRaca(grdRacas.CurrentRow.Cells(0).Value)
        TabControl1.SelectTab(0)
        txtNome.Focus()

    End Sub

    Private Sub btnNovo_Click(sender As System.Object, e As System.EventArgs) Handles btnNovo.Click
        limpar.Limpar(Me)
        txtNome.Focus()
    End Sub

    Private Sub btnExcluir_Click(sender As System.Object, e As System.EventArgs) Handles btnExcluir.Click
        comando.CommandText = "DELETE FROM racas WHERE idraca = @idRaca"
        comando.Parameters.Add("@idRaca", txtCod.Text)
        bd.atualizarDados(comando)
        limpar.Limpar(Me)
    End Sub
End Class